package plc.language;
import java_cup.runtime.*;
      
%%
  
%public
%class Lexer
%line
%column
%cup

%{   
   
    private Symbol symbol(int type) {
        return new Symbol(type, yyline, yycolumn);
    }
    private Symbol symbol(int type, Object value) {
        return new Symbol(type, yyline, yycolumn, value);
    }
%}
   

LineTerminator = \r|\n|\r\n
WhiteSpace     = {LineTerminator} | [ \t\f]
dec_int_lit = 0 | [1-9][0-9]*
dec_int_id = [A-Za-z_][A-Za-z_0-9]*
time_24 = (([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])
day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday

%%
   
<YYINITIAL> {
    ";"                 { return symbol(sym.SEMI); }
    "+"                 { return symbol(sym.PLUS); }
    "-"                 { return symbol(sym.MINUS); }
    "*"                 { return symbol(sym.TIMES); }
    "/"                 { return symbol(sym.DIVIDE); }
    "("                 { return symbol(sym.LPAREN); }
    ")"                 { return symbol(sym.RPAREN); }
    "{"                 { return symbol(sym.LBRACE); }
    "}"                 { return symbol(sym.RBRACE); }
    //"prefix"            { return symbol(sym.PREFIX); }
    //"infix"             { return symbol(sym.INFIX); }
    //"postfix"           { return symbol(sym.POSTFIX); }
    "="                 { return symbol(sym.EQUAL); }
    "<"                 { return symbol(sym.LT); }
    ">"                 { return symbol(sym.GT); }
    "<="                { return symbol(sym.LT_EQUAL); }
    ">="                { return symbol(sym.GT_EQUAL); }
   
    "call_func"         { return symbol(sym.CALL_FUNC); }
  /* keywords */
    //"boolean"           { return symbol(sym.BOOLEAN); }
    //"char"              { return symbol(sym.CHAR); }
    //"float"             { return symbol(sym.FLOAT); }
    //"int"               { return symbol(sym.INT); }

    "device"            { return symbol(sym.DEVICE); }
    "watt"              { return symbol(sym.WATT); }
    "button"            { return symbol(sym.BUTTON); }
    "if"                { return symbol(sym.IF); }
    "then"              { return symbol(sym.THEN); }
    "else"              { return symbol(sym.ELSE); }
    "and"               { return symbol(sym.AND); }
    "or"                { return symbol(sym.OR); }
    "is"                { return symbol(sym.IS); }
    "on"                { return symbol(sym.ON); }
    "off"               { return symbol(sym.OFF); }
    "turn"              { return symbol(sym.TURN); }
    "total_watt"        { return symbol(sym.TOTAL_WATT); }
    "timer"             { return symbol(sym.TIMER); }
    "at"                { return symbol(sym.AT); }
    {time_24}           { return symbol(sym.TIME); }
    "everyday"          { return symbol(sym.EVERYDAY); }
    "every"             { return symbol(sym.EVERY); }
    {day}               { return symbol(sym.DAY); }
    "function"          { return symbol(sym.FUNCTION); }
    "Baht"              { return symbol(sym.BAHT); }
    "per"               { return symbol(sym.PER); }
    "watt_hour"         { return symbol(sym.WATT_HOUR); }

    {dec_int_lit}       { return symbol(sym.NUMBER, new Integer(yytext())); }
    {dec_int_id}        { return symbol(sym.ID, new String(yytext()));}
    {WhiteSpace}        { /* just skip what was found, do nothing */ }
}
[^]                    { throw new Error("Illegal character <"+yytext()+">"); }
